package com.victor.dao;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.victor.entity.Advertisement;
import com.victor.entity.Answer;
import com.victor.entity.Comment;
import com.victor.entity.Fans;
import com.victor.entity.FollowQuestion;
import com.victor.entity.FollowUser;
import com.victor.entity.Label;
import com.victor.entity.Question;
import com.victor.entity.User;

@Mapper
public interface QuestionDao {

	/**
	 * 提交提问
	 * 
	 * @param question
	 * @return
	 */
	@Insert({ "INSERT INTO question (userid,q_title, q_content, q_reward, q_filepath, q_type, q_time) VALUES (#{userid},#{q_title},#{q_content},#{q_reward},#{q_filepath},#{q_type},NOW())" })
	int submit(Question question);

	/**
	 * 查询提问列表
	 * 
	 * @return
	 */
	@Select({ "SELECT * FROM question ORDER BY q_time DESC" })
	List<Question> getall();

	/**
	 * 通过id查询提问详情
	 * 
	 * @param qid
	 * @return
	 */
	Question findByid(@Param("qid") int qid);

	/**
	 * 手机端获取首页提问列表
	 * 
	 * @return
	 */
	List<Question> getm_all();

	/**
	 * 粉丝列表
	 * 
	 * @param userid
	 * @return
	 */
	@Select({ "SELECT * FROM fans WHERE userid=#{userid} ORDER BY id DESC" })
	List<Fans> getFansList(@Param("userid") String userid);

	/**
	 * 采纳答案
	 * 
	 * @param a_id
	 * @return
	 */
	@Update({ "UPDATE answer SET ifadopt='1' WHERE id=#{a_id}" })
	int adoptAnswer(@Param("a_id") String a_id);

	/**
	 * 通过q_id查询该提问是否有采纳的答案
	 * 
	 * @param q_id
	 * @return
	 */
	@Select({ "SELECT * FROM answer WHERE q_id=#{q_id} AND ifadopt='1'" })
	List<Answer> ifadoptAnswer(@Param("q_id") String q_id);

	/**
	 * 回答提问
	 * 
	 * @param answer
	 * @return
	 */
	@Insert({ "INSERT INTO answer (q_id,a_userid,a_content,a_filepath,a_time,ifadopt) VALUES (#{q_id},#{a_userid},#{a_content},#{a_filepath},NOW(),'0')" })
	int answerQuestion(Answer answer);

	/**
	 * 发布评论(单文字)
	 * 
	 * @param comment
	 * @return
	 */
	@Insert({ "INSERT INTO comment (a_id,c_userid,c_type,c_content,c_time) VALUES (#{a_id},#{c_userid},#{c_type},#{c_content},NOW())" })
	int releaseComment(Comment comment);

	/**
	 * 我的--关注--问题(我关注的问题)
	 * 
	 * @param userid
	 * @return
	 */
	@Select({ "SELECT * FROM follow_question WHERE userid=#{userid} ORDER BY f_time DESC" })
	List<FollowQuestion> getFollowQuestion(@Param("userid") String userid);

	/**
	 * 我的--关注--用户(我关注的用户)
	 * 
	 * @param userid
	 * @return
	 */
	@Select({ "SELECT * FROM follow_user WHERE userid=#{userid} ORDER BY f_time DESC" })
	List<FollowUser> getFollowUser(@Param("userid") String userid);

	/**
	 * 我的--提问(查询所有我的提问)
	 * 
	 * @param userid
	 * @return
	 */
	List<Question> getQuestionByMe(@Param("userid") String userid);

	/**
	 * 我的--回答(查询我回答过的提问)
	 * 
	 * @param userid
	 * @return
	 */
	List<Question> getAnswersByMe(@Param("userid") String userid);

	/**
	 * 获取广告列表
	 * 
	 * @return
	 */
	@Select({ "SELECT * FROM advertisement ORDER BY id DESC" })
	List<Advertisement> getAdvertisements();

	/**
	 * 获取某条回答的评论列表(时间排序)
	 * 
	 * @param a_id
	 * @return
	 */
	@Select({ "SELECT * FROM comment WHERE a_id=#{a_id} ORDER BY c_time DESC" })
	List<Comment> getCommentByAnswerId(@Param("a_id") String a_id);

	/**
	 * 待解答页(查询所有没有回答的提问)
	 * 
	 * @return
	 */
	List<Question> getQuestionsToNoAnswers();

	/**
	 * 用户页(查询用户信息和发布的所有提问)
	 * 
	 * @param userid
	 * @return
	 */
	List<Question> getAllQuestionByUserid(@Param("userid") String userid);

	/**
	 * 模糊查询(返回提问列表 和 回答人数)
	 * 
	 * @param str
	 * @return
	 */
	List<Question> likeQuery(@Param("str") String str);

	/**
	 * 修改个人信息
	 * 
	 * @param user
	 * @return
	 */
	int updatePersonalData(User user);

	/**
	 * 通过userid查询user表中是否存在该用户
	 * 
	 * @param userid
	 * @return
	 */
	@Select({ "select * from user where userid=#{userid}" })
	User getUserByUserid(@Param("userid") String userid);

	/**
	 * user表中不存在该用户 则插入
	 * 
	 * @param user
	 * @return
	 */
	@Insert({ "insert into user (headphoto,username,selfintroduce,identity,integral,userid) values (#{headphoto},#{username},#{selfintroduce},#{identity},'0',#{userid})" })
	int insertPersonalData(User user);

	/**
	 * 通过a_id查询回答信息
	 * 
	 * @param a_id
	 * @return
	 */
	@Select({ "select * from answer where id=#{a_id}" })
	Answer findByidToAnswer(@Param("a_id") String a_id);

	/**
	 * 查询提问列表(日期筛选)
	 * 
	 * @param starttime
	 * @param endtime
	 * @return
	 */
//	@Select({ "SELECT * FROM question WHERE q_time between #{starttime} and #{endtime}" })
	List<Question> getQuestionBySearch(@Param("starttime") String starttime, @Param("endtime") String endtime, @Param("tiaojian") String tiaojian, @Param("labels") String [] labels);

	/**
	 * 通过lable去表中查询是否有该标签
	 * 
	 * @param label
	 * @return
	 */
	@Select("select * from label where label=#{label}")
	Label selectLabel(@Param("label") String label);

	/**
	 * 标签数量加1
	 * 
	 * @param label_data
	 * @return
	 */
	@Update("update label set number=#{number} where id=#{id}")
	int updateLabelNumber(Label label);

	/**
	 * 添加标签
	 * 
	 * @param label_data
	 */
	@Insert("insert into label (label,number) values (#{label},1)")
	int insertLabelNumber(@Param("label") String label);

	/**
	 * 获取热门标签
	 * 
	 * @return
	 */
	@Select("SELECT * FROM label ORDER BY number DESC")
	List<Label> getAllLable();

	/**
	 * 修改
	 * 
	 * @param question
	 * @return
	 */
	boolean update(Question question);

	/**
	 * 批删
	 * 
	 * @param ids
	 * @return
	 */
	boolean batchDelete(@Param("ids") int[] ids);

}
